// pages/livePlayer/activitySet/index.js
const app = getApp();
const config = require("../../../utils/config");
import { http } from "../../../utils/request";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    po: "", //房间号
    rewardArr: ["红包", "积分", "不设置活动"],
    rewardType: 0, //奖励类型
    packetArr: ["手气红包", "等分红包"],
    packetType: 1, //红包类型 默认等分
    packetNum: "", //红包数量
    packetMoney: "", //红包单个金额
    packetTotal: "", //红包总金额
    packetWish: "", //红包祝福语
    packetTit: "", //未获标题
    packetTips: "", //未获提示语
    integralNum: "", //设置积分数

    openFlag: "",  //是否开启活动设置   0未开启，1已开启
    isHave: false //是否设置过活动
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      po: options.po
    })
    this.queryDetails();
  },

  /** 查询活动详情 */
  queryDetails() {
    let that = this;
    http.requestData("GET", "ActiveConfig/Get", {
      po: this.data.po
    }).then(res=>{
      if (res.Tag == 10) {
        that.setData({
          openFlag: res.Extend.IsActive
        })
        if (res.Data) {
          let data = res.Data;
          that.setData({
            isHave: true,
            rewardType: data.Type == 10 ? 0 : (data.Type == 20?1:2),
            packetType: data.RedType == 10 ? 0 : 1,
            packetNum: data.RedNum,
            packetMoney: data.Redsingle,
            packetTotal: data.RedTotalAmount,
            packetWish: data.Bless,
            packetTit: data.Untitle,
            packetTips: data.Untip,
            integralNum: data.IntegralNum
          })
        }
      } else {
        http.showToast(res.Message)
      }
    })
  },

  /** 初始化数据 */
  initVal() {
    this.setData({
      integralNum: "",
      packetNum: "",
      packetMoney: "",
      packetTotal: "",
      packetWish: "",
      packetTit: "",
      packetTips: "",
    })
  },

  /** 奖励类型 */
  changeRewardTap() {
    let that = this;
    wx.showActionSheet({
      itemList: this.data.rewardArr,
      success(e) {
        that.initVal();
        that.setData({
          rewardType: e.tapIndex
        })
      }
    })
  },
  /** 红包类型 */
  changePacketTap() {
    return;
    let that = this;
    wx.showActionSheet({
      itemList: this.data.packetArr,
      success(e) {
        // that.initVal();
        that.setData({
          packetType: e.tapIndex,
          packetMoney: "", //红包单个金额
          packetTotal: "", //红包总金额
        })
      }
    })
  },
  /** 红包数量 */
  inputPacketNum(e) {
    let value = e.detail.value;
    this.setData({
      packetNum: value.replace(/\D/g,'')
    })
    if (this.data.packetType == 1 && this.data.packetNum>0 && this.data.packetMoney>0) {
      let total = this.data.packetNum * this.data.packetMoney
      this.setData({
        packetTotal: total.toFixed(2)
      })
    }else{
      this.setData({
        packetTotal: 0
      })
    }
  },
  /** 红包单个金额 */
  inputPacketMoney(e) {
    let value = e.detail.value;
    value = value.replace(/\,/g, ".");//兼容ios部分版本 "." 显示成 ',' 的问题
    if (value.indexOf('.') == 0) { //第一位就是 .
      value = "0" + value;
    }
    value = value.replace(/\.{2,}/g, "."); 
    value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", "."); //只保留第一个点， 清除多余的点
    value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
    this.setData({
      packetMoney: value
    })
    if (this.data.packetType == 1 && this.data.packetNum>0 && this.data.packetMoney>0) {
      let total = this.data.packetNum * this.data.packetMoney
      this.setData({
        packetTotal: total.toFixed(2)
      })
    }else{
      this.setData({
        packetTotal: 0
      })
    }
  },
  /** 红包总金额 */
  inputPacketTotal(e) {
    this.setData({
      packetTotal: e.detail.value
    })
  },
  /** 红包祝福语 */
  inputPacketWish(e) {
    this.setData({
      packetWish: e.detail.value
    })
  },
  /** 未获红包标题 */
  inputPacketTit(e) {
    this.setData({
      packetTit: e.detail.value
    })
  },
  /** 未获红包提示语 */
  inputPacketTips(e) {
    this.setData({
      packetTips: e.detail.value
    })
  },
  /** 设置积分数 */
  inputSetIntegral(e) {
    this.setData({
      integralNum: e.detail.value
    })
  },

  /** 保存 */
  saveTap() {
    if (this.data.rewardType == 0) {//红包类型
      if (!this.data.packetNum || this.data.packetNum==0) {
        return http.showToast("请输入红包数量");
      }
      if (this.data.packetType == 1) {//等分红包中的单个红包金额
        if(!this.data.packetMoney || this.data.packetMoney < 0.3){
          return http.showToast("单个红包金额不能低于0.3元");
        }
      }
      if (!this.data.packetTotal || this.data.packetTotal==0) {
        return http.showToast("请输入红包总金额");
      }
      if(this.data.packetTotal<1){
        return http.showToast("总金额不能小于1元");
      }
    }
    if (this.data.rewardType == 1 && !this.data.integralNum) { //积分类型
      return http.showToast("请设置积分数");
    }
    this.submit();
    // if(this.data.rewardType == 0){//红包 先支付 后保存
    //   this.wxPay();
    // }else{//积分直接保存活动
    //   this.submit();
    // }
  },
  /** 支付红包金额 WeChat/GoPay */
  wxPay() {
    let that = this;
    http.requestData("GET", "WeChat/GoPayTongLian", {
      po: this.data.po,
      money: this.data.packetTotal
    }).then(res=>{
      if (res.Tag == 10) {
        res.Data.success = () => { //支付成功
          //that.submit(); //去保存活动
          http.showToast("设置保存成功");
          let pages = getCurrentPages();
          // pages[pages.length-1] 当前界面为null 通联支付， 否则为微信支付
          setTimeout(() => {
            wx.navigateBack({
              delta: pages[pages.length-1]?1:2
            });
          }, 500);
        },
        res.Data.fail = () => {
          http.showToast("支付失败");
          //删除保存的活动
          that.deleteActive();
        }
        //调用微信支付
        wx.requestPayment(res.Data);
      } else {
        http.showToast(res.Message);
        that.deleteActive();
      }
    })
  },
  /** 请求提交保存 */
  submit() {
    let that = this;
    http.requestData("POST", "ActiveConfig/Save", {
      po: this.data.po, //房间编号
      type: this.data.rewardType==0?10:(this.data.rewardType==1?20:30), //活动类型(10-红包 20-积分 30-不设置)
      integralNum: this.data.integralNum, //积分数
      redType: this.data.packetType == 0 ? 10 : 20, //红包类型(10-手气 20-等分)
      redTotalAmount: this.data.packetTotal, //红包总金额
      redNum: this.data.packetNum, //红包个数
      redSingle: this.data.packetMoney, //单个红包金额
      bless: this.data.packetWish, //红包祝福语
      unTitle: this.data.packetTit, //未获得红包标题
      unTip: this.data.packetTips, //未获得红包提示语
    },true).then(res=>{
      if (res.Tag == 10) {
        if (that.data.rewardType == 0 || that.data.rewardType == 10) { //红包
          http.showToast("支付中..");
          that.wxPay();
        }else {
          http.showToast(res.Message);
          //刷新直播间
          // let pages = getCurrentPages();
          // pages[pages.length-2].setData({
          //   isRefresh: true
          // })
          setTimeout(() => {
            wx.navigateBack();
          }, 200);
        }
      }else{
        http.showToast(res.Message);
      }
    })
  },

  /** 支付失败，则删除保存的活动 */
  deleteActive() {
    http.requestData("POST", "ActiveConfig/Delete", {
      po: this.data.po,
      type: 10 //红包
    }).then(res=>{
      if(res.Tag==10){
        console.log("活动删除成功")
      }else{
        console.log("活动删除失败")
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {}
})